package com.woniuxy.bookmallmanagersrever.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.bookmallmanagersrever.entity.TManagerRole;
import com.woniuxy.bookmallmanagersrever.entity.TRole;
import com.woniuxy.bookmallmanagersrever.entity.TUrlPermission;
import com.woniuxy.bookmallmanagersrever.mapper.TManagerRoleMapper;
import com.woniuxy.bookmallmanagersrever.mapper.TRoleMapper;
import com.woniuxy.bookmallmanagersrever.mapper.TRoleUrlPermissionMapper;
import com.woniuxy.bookmallmanagersrever.vo.RolePerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RoleService {
    @Autowired
    TRoleMapper  roleMapper ;
    @Autowired
    TRoleUrlPermissionMapper roleUrlPermissionMapper ;

    @Autowired
    TManagerRoleMapper managerRoleMapper;

    public PageInfo<TRole> findRoleByPage(Integer now, Integer size, String name) {
        PageHelper.startPage(now,size);
        List<TRole> lists = roleMapper.selectRoleByName(name);
        return new PageInfo<>(lists);
    }

    public void add(TRole role) {
        role.setInsertTime(new Date());
        roleMapper.insert(role) ;
    }

    public void modify(TRole role) {
        role.setUpdateTime(new Date());
        roleMapper.updateByPrimaryKey(role);
    }

    @Transactional
    public void removeByKey(int id) {
        //删除 角色 权限表中的 数据
        roleUrlPermissionMapper.deleteDataByRoleId(id);
        //删除u 用户 角色表中的数据
        managerRoleMapper.deleteDataByRoleId(id);
        //删除 角色数据
        roleMapper.deleteByPrimaryKey(id);
    }

    public void addGrante(RolePerVo rolePerVo) {
        //先删除 当前角色的所有权限
            //???
        //添加薪的权限
        roleUrlPermissionMapper.insertRoleAndPer(rolePerVo.getId(),rolePerVo.getCheckedKeys());
    }

    public List<TRole> findAllRoles() {
        return roleMapper.selectAll();
    }
}
